package com.party.admin.biz.logistics;

import com.alibaba.fastjson.JSONObject;
import com.party.api.dto.logistics.response.LogisticsResponse;
import com.party.api.dto.logistics.response.SubscribeResponse;
import com.party.api.dto.logistics.response.Traces;
import com.party.common.utils.StringUtils;
import com.party.core.model.logistics.WlLogistics;
import com.party.core.model.logistics.WlLogisticsStatus;
import com.party.core.model.logistics.WlParcelOutput;
import com.party.core.service.logistics.IWlLogisticsService;
import com.party.core.service.logistics.IWlLogisticsTraceService;
import com.party.core.service.logistics.IWlParcelService;
import com.party.core.service.logistics.biz.LogisticsBizService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
 * 物流业务处理
 * Created by yifeng
 *
 * @date 2018/6/1 0001
 * @time 下午 15:04
 */
@Service
public class WlLogisticsBizService {
    @Autowired
    private IWlLogisticsService wlLogisticsService;
    @Autowired
    private IWlLogisticsTraceService wlLogisticsTraceService;
    @Autowired
    private IWlParcelService wlParcelService;
    @Autowired
    private LogisticsBizService logisticsBizService;

    /**
     * 保存订阅推送数据
     *
     * @param requestData 推送数据
     * @throws Exception
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void saveNotifyData(String requestData, Function<String, Object> function) throws Exception {
        SubscribeResponse subscribeResponse = JSONObject.parseObject(requestData, SubscribeResponse.class);
        for (LogisticsResponse response : subscribeResponse.getData()) {
            String callBack = response.getCallBack();
            String logisticsId = "";
            String orderId = "";
            if (StringUtils.isNotEmpty(callBack)) {
                Map<String, Object> callBackMap = JSONObject.parseObject(callBack, Map.class);
                logisticsId = callBackMap.get("logisticsId") != null ? callBackMap.get("logisticsId").toString() : "";
                orderId = callBackMap.get("orderId") != null ? callBackMap.get("orderId").toString() : "";
            }
            if (StringUtils.isNotEmpty(logisticsId) && response.getSuccess().equals("true")) {
                WlLogistics mallLogistics = wlLogisticsService.get(logisticsId);
                // 物流
                if (mallLogistics == null) {
                    continue;
                } else {
                    mallLogistics.setStatus(Integer.valueOf(response.getState())); // 状态
                    wlLogisticsService.update(mallLogistics);
                }

                wlLogisticsTraceService.deleteByLogisticsId(logisticsId);
                for (Traces traces : response.getTraces()) {
                    logisticsBizService.insertTrace(logisticsId, traces);
                }
            }

            Integer signCount = 0;
            List<WlParcelOutput> parcelOutputs = wlParcelService.findByTargetId(orderId);
            for (WlParcelOutput parcelOutput : parcelOutputs) {
                if (parcelOutput.getStatus().equals(WlLogisticsStatus.HAVE_SIGNED.getCode())) {
                    signCount++;
                }
            }
            if (signCount == parcelOutputs.size()) {
                function.apply(orderId);
            }
        }
    }
}
